home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Devices and Hardware / ADB / ControlKeyPatch / ControlKeyTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  4.0 KB  |  153 lines  |  [TEXT/MPS ]

  1. /*    File:        ControlKeyTest.c
  2.     
  3.     Description: 
  4.              routines for patching the ADB manager to simulate the control
  5.             key being held down.  This file contains a simple application that
  6.             installs the control key patch and allows users to turn it on and off.
  7.  
  8.     Author:    John Montbriand
  9.  
  10.     Copyright: 
  11.             Copyright © 1999 by Apple Computer, Inc.
  12.             All rights reserved worldwide.
  13.     
  14.     Disclaimer:
  15.             You may incorporate this sample code into your applications without
  16.             restriction, though the sample code has been provided "AS IS" and the
  17.             responsibility for its operation is 100% yours.  However, what you are
  18.             not permitted to do is to redistribute the source as "DSC Sample Code"
  19.             after having made changes. If you're going to re-distribute the source,
  20.             we require that you make it clear in the source that the code was
  21.             descended from Apple Sample Code, but that you've made changes.
  22.     
  23.     Change History (most recent first):
  24.             27/8/99 created by John Montbriand
  25. */
  26.  
  27. #include <Types.h>
  28. #include <QuickDraw.h>
  29. #include <Menus.h>
  30. #include <Windows.h>
  31. #include <Dialogs.h>
  32. #include <Events.h>
  33. #include <Fonts.h>
  34. #include <SegLoad.h>
  35. #include <Resources.h>
  36. #include <Gestalt.h>
  37. #include <Appearance.h>
  38.  
  39. #include "ControlKeyPatch.h"
  40.  
  41.     /* true while the app is running */
  42. Boolean gRunning = true;
  43.  
  44.     /* true while the app is in forground */
  45. Boolean gForground = true;
  46.  
  47.     /* true while the control key is locked down */
  48. Boolean gLocked = false;
  49.  
  50.     /* a pointer to the patch - type defined in ControlKeyPatch.h*/
  51. ControlKeyPatchPtr gPatch = NULL;
  52.  
  53.     /* QuickDraw globals*/
  54. #ifndef __MWERKS__
  55. QDGlobals    qd;
  56. #endif
  57. Boolean gAppearance = false;
  58.  
  59. int main(void) {
  60.     OSErr err;
  61.     ControlHandle lockControl;
  62.     DialogPtr mainDialog;
  63.     long response;
  64.         /* set up our app */
  65.     SetApplLimit(GetApplLimit());
  66.     MaxApplZone();
  67.     InitGraf(&qd.thePort);
  68.     InitFonts();
  69.     InitWindows();
  70.     TEInit();
  71.     InitMenus();
  72.     InitDialogs(0);
  73.     FlushEvents(everyEvent, 0);
  74.     InitCursor();
  75.     if (Gestalt(gestaltAppearanceAttr, &response) != noErr) response = 0;
  76.     if ((response & (1<<gestaltAppearanceExists)) != 0) {
  77.         err = RegisterAppearanceClient();
  78.         if (err != noErr) goto bail;
  79.         gAppearance = true;
  80.     }
  81.     
  82.         /* set up the dialog */
  83.     mainDialog = GetNewDialog(128, NULL, (WindowPtr) (-1));
  84.     {    short itemt;
  85.         Rect itemb;
  86.         GetDialogItem(mainDialog, 1, &itemt, (Handle*) &lockControl, &itemb);
  87.     }
  88.     
  89.         /* install the patch */
  90.     err = NewControlKeyPatch(false, &gPatch);
  91.     if (err != noErr) goto bail;
  92.  
  93.         /* run the app */
  94.     while (gRunning) {
  95.         EventRecord ev;
  96.         DialogPtr theDialog;
  97.         WindowPtr theWindow;
  98.         short itemNo;
  99.             
  100.             /* get the next event */
  101.         if ( ! WaitNextEvent(everyEvent, &ev,  GetCaretTime(), NULL))
  102.             ev.what = nullEvent;
  103.             /* track forground switches */
  104.         if ((ev.what == osEvt) && (((ev.message >> 24) & 0x0FF) == suspendResumeMessage)) {
  105.             gForground = ((ev.message & resumeFlag) != 0);
  106.             DrawDialog(mainDialog);
  107.             HiliteControl(lockControl, gForground ? 0 : 255);
  108.         }
  109.             /* redraw if we're activating */
  110.         if (ev.what == activateEvt) {
  111.             DrawDialog(mainDialog);
  112.             HiliteControl(lockControl, ((ev.modifiers & activeFlag) != 0) ? 0 : 255);
  113.         }
  114.             /* handle clicks in the dialog window */
  115.         if (IsDialogEvent(&ev))
  116.             if (DialogSelect(&ev, &theDialog, &itemNo)) {
  117.                 gLocked = ! gLocked;
  118.                 SetControlValue(lockControl, (gLocked ? 1 : 0));
  119.                 if (gLocked)
  120.                     SetControlKeyDown(gPatch);
  121.                 else SetControlKeyUp(gPatch);
  122.             }
  123.             /* process other window commands */
  124.         if (ev.what == mouseDown)
  125.             switch (FindWindow(ev.where, &theWindow)) {
  126.                     
  127.                     /* clicks in the close box, close the app */
  128.                 case inGoAway:
  129.                     if (TrackGoAway(theWindow, ev.where))
  130.                         gRunning = false;
  131.                     break;
  132.                     
  133.                     /* allow window drags */
  134.                 case inDrag:
  135.                     {    Rect boundsRect = { -32000, -32000, 32000, 32000};
  136.                         DragWindow(theWindow, ev.where, &boundsRect);
  137.                     }
  138.                     break;
  139.                     
  140.                     /* desktop clicks, etc... */
  141.                 case inSysWindow:
  142.                     SystemClick(&ev, theWindow);
  143.                     break;
  144.             }
  145.     }
  146.     
  147. bail:
  148.     if (gPatch != NULL) DisposeControlKeyPatch(gPatch);
  149.     if (gAppearance)
  150.         UnregisterAppearanceClient();
  151.     ExitToShell();
  152.     return 0;
  153. }